// 日志存储
const log4js = require('log4js')

const levels = {
    'trace': log4js.levels.TRACE,
    'debug': log4js.levels.DEBUG,
    'info': log4js.levels.INFO,
    'warn': log4js.levels.WARN,
    'error': log4js.levels.error,
    'fatal': log4js.levels.FATAL,
}

log4js.configure({
    appenders: {
        console: {
            type: 'console'
        },
        debug:{
            type:'dateFile',
            filename:'logs/debug/log',
            pattern:'yyyy-MM-dd.log',
            alwaysIncludePatttern:true //设置文件名称为 filename+pattern
        },
        info:{
            type:'file',
            filename:'all-logs.log'
        },
        error:{
            type:'dateFile',
            filename:'logs/log',
            pattern:'yyyy-MM-dd.log',
            alwaysIncludePatttern:true //设置文件名称为 filename+pattern
        }
    },
    categories: {
        default: {
            appenders: ['console'],
            level: 'debug'
        },
        debug:{
            appenders:['debug','console'],
            level:'info'
        },
        info:{
            appenders:['info','console'],
            level:'info'
        },
        error:{
            appenders:['error','console'],
            level:'error'
        }
    }
})


/**
 * lever为debug的日志输出
 * @param {string} content 
 */
exports.debug = (content)=>{
    let logger = log4js.getLogger('debug') //参数值先会在categories中找，找不到就会默认使用default对应的appenders
    logger.level = levels.debug
    logger.debug(content)
}

/**
 * lever为info的日志输出
 * @param {string} content 
 */
 exports.info = (content)=>{
    let logger = log4js.getLogger('info')
    logger.level = levels.info
    logger.info(content)
}

/**
 * lever为error的日志输出
 * @param {string} content 
 */
 exports.error = (content)=>{
    let logger = log4js.getLogger('error')
    logger.level = levels.error
    logger.error(content)
}